package com.example.shouybl.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.GiftMobileAdapter;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.bean.GiftData;
import com.example.shouybl.constant.Path;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftWebFragment extends Fragment {


    private ListView lvGiftWeb;
    private List<GiftData> dataList= new ArrayList<GiftData>();;
    private AsyncHttpClient client = new AsyncHttpClient();
    private GiftMobileAdapter mobileAdapter;
    private int page = 1;
    private PullToRefreshListView pullWeb;
    private AppBaseBean baseBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_gift_web, container, false);
        initView(v);
        initData();
        pullWeb.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                initData();
            }
        });
        lvGiftWeb.setAdapter(mobileAdapter);
        return v;
    }

    private void initData() {

        client.post(Path.PATH_GIFT, getParams(), new TextHttpResponseHandler() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                baseBean= com.alibaba.fastjson.JSONObject.parseObject(responseString, AppBaseBean.class);
                if (baseBean!=null && baseBean.getState().equals("success")){
                    if (page==1 && !dataList.isEmpty()){
                        dataList.clear();
                    }
                    dataList.addAll(baseBean.getInfo());
                }
                mobileAdapter.notifyDataSetChanged();
                pullWeb.onRefreshComplete();
            }
        });
    }
    private RequestParams getParams(){
        RequestParams params=new RequestParams();
        params.add("uid", "");
        params.add("token", "78fc45f6f%2C7%2C9");
        params.add("platform", "2");
        params.add("gifttype", "2");
        params.add("page", String.valueOf(page));
        return params;
    }



    private void initView(View v) {
        pullWeb = (PullToRefreshListView) v.findViewById(R.id.pull_gift_web);
        pullWeb.setMode(PullToRefreshBase.Mode.BOTH);
        lvGiftWeb=pullWeb.getRefreshableView();
        mobileAdapter = new GiftMobileAdapter(dataList, BaseApp.getInstance());
    }


}
